/*************************
新增编辑事件
* */
<template>
  <div>
    <el-dialog
      :title="dialogTitle"
      :close-on-click-modal="false"
      :visible.sync="visible"
      width="70vw"
      top="8vh"
      @close="closeJgDialog()"
    >
      <div :height="searchheight" v-loading.fullscreen.lock="fullscreenLoading">
      <span
        style="float: right; margin-top: -30px; cursor: pointer"
        @click="searchchangeadvanced"
      >{{ searchadvanced ? "收起" : "展开" }}
        <i
          :class="searchadvanced ? 'el-icon-arrow-up' : 'el-icon-arrow-down'"
        />
      </span>
        <div v-show="searchadvanced">
          <div class="boxsty">
            <el-form
              :model="dataForm"
              :rules="dataRule"
              ref="dataForm"
              @keyup.enter.native="dataFormSubmit()"
              label-width="130px"
            >
             <!-- <div class="boxsty">-->
                <el-row>
                  <el-col :span="24">
                    <el-col :span="15">

                      <el-form-item label="标题:" prop="sjbt">
                        <el-input v-model="dataForm.sjbt" placeholder="标题" clearable max-length="20"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col  :span="9">
                      <el-form-item label="考核周期:" prop="bmkhzqId">
                        <!--新增-->
                        <el-select
                          v-if="editShow == 0"
                          v-model="dataForm.bmkhzqId"
                          placeholder="请选择周期"
                          class="xiaLa"
                          style="width: 100%"
                          :clearable="true"
                          @change="selecteRefreshKhzq"
                        >
                          <el-option
                            v-for="(item, index) in kezqsList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                          ></el-option>
                        </el-select>
                        <!--修改-->
                        <el-select
                          v-else
                          v-model="dataForm.bmkhzqId"
                          placeholder="请选择周期"
                          class="xiaLa"
                          style="width: 100%"
                          :clearable="true"
                          @change="selecteRefreshKhzq"
                        >
                          <el-option
                            v-for="(item, index) in kezqsList"
                            :key="index"
                            :label="item.name"
                            :value="Number(item.value)"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                  </el-col>
                </el-row>


                <el-row>
                  <el-col>
                    <el-form-item label="描述:" prop="sjdesc">
                      <el-input type="textarea" :rows="4" v-model="dataForm.sjdesc" placeholder="描述"  max-length="10"></el-input>
                    </el-form-item>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-col :span="12">
                    <el-form-item label="模板名称:" prop="templateId">
                      <el-select
                        v-model="dataForm.templateId"
                        placeholder="请选择模板"
                        class="xiaLa"
                        style="width: 100%"
                        :clearable="true"
                        @change="selecteRefresh"
                      >
                        <el-option
                          v-for="(item, index) in templateNamesList"
                          :key="index"
                          :label="item.name"
                          :value="item.value"
                        ></el-option>
                      </el-select>
                    </el-form-item>
                    </el-col>
                  </el-col>
                </el-row>
              <!--调整项-->
            <!--  <div class="boxsty" style="border: 1px solid #ccccda;">
                <el-row>
                  <el-col :span="24">
                    <el-col :span="12">
                      <el-form-item label="类别:" prop="lb1">
                        <el-select
                          v-model="dataForm.lb1"
                          placeholder="请选择类别"
                          class="xiaLa"
                          style="width: 100%"
                          :clearable="true"
                          @change="selectChaged($event)"
                        >
                          <el-option
                            v-for="(item, index) in lbsList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>

                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-col :span="12">
                      <el-form-item label="依据:" prop="yj2">
                        <el-select
                          v-model="dataForm.yj2"
                          placeholder="请选择依据"
                          class="xiaLa"
                          style="width: 100%"
                          :clearable="true"
                          @change="yoYjget"
                        >
                          <el-option
                            v-for="(item, index) in yjsList"
                            :key="index"
                            :label="item.name"
                            :value="item.value"
                          ></el-option>
                        </el-select>
                      </el-form-item>
                    </el-col>
                    <el-col :span="6">
                      <span style="padding-left: 100px;padding-top: 20px;">指标规则：{{getZbgz}}</span>
                    </el-col>

                    <el-col  :span="6" >
                      <span style="padding-left: 80px;padding-top: 20px;">标准：{{getJkfbz}}</span>
                    </el-col>
                  </el-col>
                </el-row>
                <el-row>
                  <el-col :span="24">
                    <el-col :span="8">
                      <el-form-item label="加扣分值:" prop="jkfz">
                        <el-input v-model="dataForm.jkfz" placeholder="加扣分值" clearable max-length="10"  @input="gojkfz"></el-input>
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="奖金项:" prop="jjx">
                          <el-select
                            v-model="dataForm.jjx"
                            placeholder="请选择奖金项"
                            class="xiaLa"
                            style="width: 100%"
                            :clearable="true"
                            @change="gojjx"
                          >
                            <el-option
                              v-for="(item, index) in jjxsList"
                              :key="index"
                              :label="item.name"
                              :value="item.value"
                            ></el-option>
                          </el-select>
                          &lt;!&ndash; <el-input v-model="dataForm.jjx" placeholder="奖金项" clearable max-length="10"  @input="gojjx"></el-input>&ndash;&gt;
                      </el-form-item>
                    </el-col>
                    <el-col :span="8">
                      <el-form-item label="加扣奖金:" prop="jkjj">
                        <el-input v-model="dataForm.jkjj" placeholder="加扣奖金" clearable max-length="10"  @input="gojkjj"></el-input>
                      </el-form-item>
                    </el-col>
                  </el-col>
                </el-row>
              </div>-->
              <!--调整项结束-->
              <!--</div>-->
            </el-form>
      <!--<EventZxForm ref="eventzxRef" :eventzxForm="dataForm" :getZbgz="getZbgz" :getJkfbz="getJkfbz"></EventZxForm>-->
          </div>
        </div>
      </div>
      <EventZxjgForm ref="eventzxRef" :eventzxForm="dataForm" :editShow="editShow"></EventZxjgForm>
     <!-- <ZhuanXiangGzForm ref="zxgzRef" :zhuanxiangGzForm="dataForm"></ZhuanXiangGzForm>-->
      <span slot="footer" class="dialog-footer">

        <el-button  type="primary" plain @click="saveFormData">确定</el-button>
        <el-button type="primary" plain @click="closeJgDialog()">返回</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
  import EventZxjgForm from "@/views/dailyExamine/eventExamine/createEvent/event-zxjg-form.vue"
  import {
      addEvent,
      updateEvent,
      getKhzqSelect,
      getLbSelect,
      getMbnameSelect,
      getById,
      getKhdxList,
      getOpenKhzq,
      getKhzqName
  } from "@/api/dailyExamine/eventExamine/event.js";

    export default {
        components: {
            EventZxjgForm,
        },
        props: {
            dialogTitle: String
        },
        data() {

            var checkSjbt= (rule, value, callback) => {
                if (value) {
                    if (value.length>20) {
                        callback("最多可输入20个字符!");
                    } else {
                        callback();
                    }
                } else {
                    callback("请输入标题");
                }
            };

            var checkSjdesc = (rule, value, callback) => {
                if (value) {
                    if (value.length>250) {
                        callback("最多可输入250个字符!");
                    } else {
                        callback();
                    }
                } else {
                    callback("请输入描述");
                }
            };

            return {
                //isSubmit: false, //保存按钮的loading
                mainDept: [], //部门数组
                deletePerson: [], //删除人员数组
                deleteForm: {}, //删除弹窗中的删除人员对象
                deleteFormVisible: false, //删除弹窗显隐
                usercomment: false, //人员选择显隐
                usercommentLd: false, //人员选择显隐
                fakeData: [], //空数组
                multipleSelection: [], //选中的参与人
                zxgzCyrxxList: [], //参与人数组
                visible: false, //最外弹窗显隐
                //canSubmit: false,
                dataForm: {},
                qcJlId: "",
                qcJlRealName: "",
                innerVisible: false,
                fileListUpload: [],
                limitUpload: 3,
                accountList: [],
                eventzxForm: {},
                templateNamesList: [],
                kezqsList: [],
                lbsList: [],
                yjsList: [],
                jjxsList: [
                    { name: "奖金项1", value: "0" },
                    { name: "奖金项2", value: "1" },
                    { name: "奖金项3", value: "2" },
                ],
                tylb : "",
                khzq: "",
                getZbgz:"",
                getJkfbz:"",
                editShow: 0,
                searchadvanced: true,
                searchheight: "0",
                fullscreenLoading: false,
                dataRule: {
                    sjbt: [
                        { required: true, trigger: "blur",validator: checkSjbt}
                    ],
                    sjdesc: [
                        { required: true, trigger: "blur",validator:checkSjdesc}
                    ],
                    /*bmkhzqId:[
                        { required: true, message: "请选择考核周期", trigger: "change" },
                    ],*/
                    /*templateId: [
                        { required: true, message: "请选择模板", trigger: "change" },
                    ],*/
                }
            };
        },
        watch: {},
        created() {


        },
        methods: {
            //初始化
            csh() {
                this.dataForm = {};
            },
            init(obj) {
                if (obj == undefined) {
                    //新增
                    this.getKhzq();//考核周期
                    this.getTemplateName();//模板
                }else{
                    //修改
                    this.dataForm = obj;
                    this.editShow = 1;
                    getKhdxList(Object.assign({
                        sjkhId:obj.id
                    })).then(r => {
                        this.$refs.eventzxRef.editLock();
                        this.$refs.eventzxRef.getList(r.data.data);
                        //this.$forceUpdate();
                    });
                    this.$forceUpdate()
                    this.getXgKhzq(obj.khzq);//考核周期
                    this.getXgTemplateName();//模板
                }

                //this.getLb();//类别

                this.visible = true;
                //this.canSubmit = true;
            },
            //获取考核周期列表
            getKhzq(){
                let seld = '';
                getOpenKhzq().then(r => {
                    seld=r.data.data.id;
                    getKhzqSelect().then(r => {
                        let list=[];
                        r.data.data.forEach((ele) => {
                            let item = {
                                name:ele.period,
                                value:ele.id
                            }

                            this.kezqsList.push(item)
                            if(ele.id == seld){
                                this.dataForm.bmkhzqId = seld
                                this.khzq = ele.period

                            }

                        });
                        //this.kezqsList = list;

                    });
                })

            },
            //获取考核周期列表
            getXgKhzq(obj){
                getKhzqSelect().then(r => {
                    r.data.data.forEach((ele) => {
                        let item = {
                            name:ele.period,
                            value:ele.id
                        }

                        this.kezqsList.push(item)
                    });
                });
                this.khzq = obj
                this.$forceUpdate()
            },
            selecteRefreshKhzq(obj){

                if(obj != "" && obj != null && obj != undefined){
                    getKhzqName(obj).then(r => {
                        this.khzq = r.data.data.period;
                    })
                    this.$forceUpdate()
                }else{
                    this.dataForm.bmkhzqId = "";
                    this.$forceUpdate()
                }

            },
            selecteRefresh(){
                this.$forceUpdate()
            },
            //获取模板列表
            getTemplateName(){
                getMbnameSelect().then(r => {
                    let list=[];

                    r.data.data.forEach((ele) => {
                        let item = {
                            name:ele.templateName,
                            value:ele.id
                        }
                        this.templateNamesList.push(item);
                        if(ele.isZmb == 1){
                            this.dataForm.templateId = ele.id
                        }
                    });
                });
            },
            //获取模板列表
            getXgTemplateName(){
                getMbnameSelect().then(r => {
                    let list=[];

                    r.data.data.forEach((ele) => {
                        let item = {
                            name:ele.templateName,
                            value:ele.id
                        }
                        this.templateNamesList.push(item);


                    });
                    //this.templateNamesList = list;
                });
            },
            //获取类别列表
            getLb(){
                getLbSelect(Object.assign({
                    yjkfl: 'jg'
                })).then(r => {
                    let list=[];
                    r.data.data.forEach((ele) => {
                        let item = {
                            name:ele.fl,
                            value:ele.id
                        }
                        this.lbsList.push(item);

                    });
                    //this.lbsList = list;
                });
            },
            //联动类别，依据列表
             selectChaged(val){
                if(val !=null && val !=""){
                    //delete this.dataForm.yj2; // 清空下拉框2的值
                    //this.$set(this.dataForm, "yj2", []);
                    //this.$set(this.dataForm, 'yj2', '');
                    //delete this.dataForm.yjsList;
                      this.dataForm.yj2 = '';
                      this.yjsList = []
                     getById(val).then(r => {
                         getLbSelect(Object.assign({
                             yjkfl: 'jg',
                             fl: r.data.data.fl
                         })).then(r => {
                             let list=[];

                             r.data.data.forEach((ele) => {
                                 let item = {
                                     name:ele.yj,
                                     value:ele.id
                                 }
                                 this.yjsList.push(item);

                             });

                         });
                    });

                    if(val != null && val != '' && val != undefined){
                        this.$refs.eventzxRef.appendTolb(val);
                    }
                  }

            },
            //获取标准，指标规则
            yoYjget(val){
                //选择的时候强制刷新
                this.$forceUpdate()
                if(val !=null && val !=""){
                    getLbSelect(Object.assign({
                        yjkfl: 'jg',
                        fl: this.tylb,
                        sjkid: val

                    })).then(r => {
                        let dafor =[];
                        dafor=r.data.data;
                        dafor.forEach((ele)=>{
                            if(ele.zbgz == 1){
                                this.getZbgz ='负指标'
                            }else{
                                this.getZbgz ='正指标'
                            }
                            this.getJkfbz = ele.jkfbz;

                        })
                        if(val != null && val != '' && val != undefined){
                          this.$refs.eventzxRef.appendToyj(val);
                        }
                        this.$refs.eventzxRef.appendTozb(this.getZbgz,this.getJkfbz);
                        //获得指标规则，标准 动态绑定页面中
                    });

                }
            },
            //选择追加子组件数据3方法
            gojkfz(){
                this.$refs.eventzxRef.appendTojkfz(this.dataForm.jkfz);
            },
            gojjx(val){
                if(val != null && val != '' && val != undefined){
                    this.$refs.eventzxRef.appendTojjx(val);
                }
                //this.$refs.eventzxRef.appendTojjx(this.dataForm.jjx);
            },
            gojkjj(){
                this.$refs.eventzxRef.appendTojkjj(this.dataForm.jkjj);
            },
            searchchangeadvanced() {
                this.searchadvanced = !this.searchadvanced;
                if (this.searchadvanced) {
                    this.searchheight = "auto";
                }
            },
            //保存编辑主子表
            saveFormData() {
                //this.isSubmit = true;
                this.dataForm.sjkhdxList = this.$refs.eventzxRef.dataList;
                let flag = true;
                let lbFlag = true;
                let yjFlag = true;
                let bzFlag = true;
                let zbgzFlag = true;
                let jjxIdFlag = true;
                let jjfzNumYzFlag = true;
                let jkjjNumYzFlag = true;

                const szValid = /^[0-9]*$/

                if(this.dataForm.bmkhzqId == null || this.dataForm.bmkhzqId == "" ){
                    this.$message.warning("请先设置默认考核周期");
                    return false;
                }
                if(this.dataForm.templateId == null || this.dataForm.templateId == "" ){
                    this.$message.warning("请先设置默认模板");
                    return false;
                }
                /* let jjxIdFlag = this.dataForm.sjkhdxList.every(it =>{
                   return it.jjxId != "" && it.jjxId != null
               })
               if(!jjxIdFlag){
                   this.$message.error('奖金项不能为空');
                   return ;
               }*/

                this.dataForm.sjkhdxList.forEach((ele) => {
                    if((ele.jkfz == null || ele.jkfz =="") && (ele.jkjj == null || ele.jkjj =="")){
                        this.$message.warning("加扣分值,加扣奖金不能都为空");
                        flag=false;
                        throw new Error('end') // 终止循环
                    }

                    if (ele.lb != null) { //如果类别为空，则是导入进来的数据，不做判断

                        if (ele.lb == null || ele.lb == "") {
                            this.$message.warning("类别不能为空");
                            lbFlag = false;
                            throw new Error('end') // 终止循环
                        }

                        if (ele.yj == null || ele.yj == "") {
                            this.$message.warning("依据不能为空");
                            yjFlag = false;
                            throw new Error('end') // 终止循环
                        }

                        if (ele.bz == null || ele.bz == "") {
                            this.$message.warning("标准不能为空");
                            bzFlag = false;
                            throw new Error('end') // 终止循环
                        }
                    }


                    if(ele.zbgz == null || ele.zbgz =="" ){
                        this.$message.warning("指标规则不能为空");
                        zbgzFlag=false;
                        throw new Error('end') // 终止循环
                    }

                    if(ele.jkfz == null || ele.jkfz ==""){
                        this.$message.warning("加扣分值不能为空");
                        jjfzNumYzFlag=false;
                        throw new Error('end') // 终止循环
                    }

                    if(ele.jkjj != null && ele.jkjj !=""){

                        if (!szValid.test(ele.jkjj)) {
                            this.$message.warning("加扣奖金请输入数字");
                            jkjjNumYzFlag=false;
                            throw new Error('end') // 终止循环
                        }

                    }
                });
                this.dataForm['khtype'] = 'jg';

                this.dataForm['khzq'] = this.khzq,
                this.dataForm.sjkhdxList['khtype'] = 'jg';

                console.log("this.dataForm",this.dataForm)

                if(flag || lbFlag || yjFlag || bzFlag  || zbgzFlag  || jjxIdFlag || jjfzNumYzFlag || jkjjNumYzFlag){

                  this.$refs["dataForm"].validate(valid => {
                      if (valid) {
                          // 编辑
                          if (this.dataForm.id) {
                              //debugger;
                              this.fullscreenLoading = true;
                              updateEvent(this.dataForm).then(r => {
                                  if (r.data.code == 0) {
                                      //this.isSubmit = false;
                                      this.fullscreenLoading = false;
                                      this.$emit("refreshJgDataList");
                                      this.closeJgDialog();
                                      this.$message.success("编辑成功");
                                  }
                              });
                          } else {
                              // 新增
                              this.$confirm("确定要保存数据么", "提示", {
                                  confirmButtonText: "确定",
                                  cancelButtonText: "取消",
                                  type: "warning",
                              }).then(res=>{
                                this.fullscreenLoading = true;
                                addEvent(this.dataForm).then(r => {
                                    if (r.data.code == 0) {
                                        //this.isSubmit = false;
                                        this.fullscreenLoading = false;
                                        this.$emit("refreshJgDataList");
                                        this.closeJgDialog();
                                        this.$message.success("新增成功");
                                    }
                                })
                                .catch(() => {
                                    this.fullscreenLoading = false;
                                })
                              })
                          }
                      } else {
                          return false;
                      }
                  });
                }
            },
            closeJgDialog() {
                this.visible = false;
                this.$emit("closeJgDialog");
            }
        }
    };
</script>
<style lang="scss" scoped>
  /deep/.el-table__header-wrapper .el-checkbox {
    display: block!important;
  }
  .xiaLaTwo .el-input--small .el-input__inner {
    border: none;
    background: transparent;
  }
  .xiaLaTwo .el-icon-arrow-up:before {
    content: "";
  }
  .el-divider {
    background-color: #fff;
  }
  .tree-box {
    height: 500px;
    overflow: auto;
  }
  .inputDeep{
    border: 0;
  }
</style>
